package com.authine.cloudpivot.ext.utils;

import com.alibaba.cola.dto.SingleResponse;
import com.alibaba.cola.exception.Exceptions;
import com.authine.common.util.ObjectMapperUtils;
import com.authine.mvp.app.integrate.dto.domainmodel.base.ErrCode;
import com.authine.mvp.app.launcher.dto.bo.response.BO;
import com.authine.mvp.app.launcher.dto.bo.response.BOList;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.util.List;
import java.util.Map;


/**
 * @ClassName TypeReferenceUtils
 * @Description: 通过json进行泛型转换
 * @Author fj
 * @Date 2021/8/18
 * @Version V1.0
 **/
public class TypeReferenceUtils {

    /**
     * json字符串转为SingleResponse泛型对象
     *
     * @param content json字符串
     * @return 转换后的对象
     */
    public static SingleResponse<BO> readValueToSingleResponse(String content) {
        SingleResponse<BO> bo = null;
        try {
            bo = new ObjectMapper().readValue(content, new TypeReference<SingleResponse<BO>>() {

            });
        } catch (JsonProcessingException e) {
            e.printStackTrace();
            Exceptions.throwBizException(ErrCode.JSON_PASE_ERR);
        }
        return bo;
    }


    public static SingleResponse<BOList> readValueToSingleResponseList(String content) {
        SingleResponse<BOList> bo = null;
        try {
            bo = new ObjectMapper().readValue(content, new TypeReference<SingleResponse<BOList>>() {

            });
        } catch (JsonProcessingException e) {
            e.printStackTrace();
            Exceptions.throwBizException(ErrCode.JSON_PASE_ERR);
        }
        return bo;
    }

    public static SingleResponse readValueToSingleRes(String content) {
        SingleResponse bo = null;
        try {
            bo = new ObjectMapper().readValue(content, new TypeReference<SingleResponse>() {

            });
        } catch (JsonProcessingException e) {
            e.printStackTrace();
            Exceptions.throwBizException(ErrCode.JSON_PASE_ERR);
        }
        return bo;
    }


    /**
     * json字符串转为list型对象
     *
     * @param content json字符串
     * @return 转换后的对象
     */
    public static List<Map<String, Object>> readValueToList(String content) {
        List<Map<String, Object>> list = null;
        try {
            list = new ObjectMapper().readValue(content, new TypeReference<List<Map<String, Object>>>() {

            });
        } catch (JsonProcessingException e) {
            e.printStackTrace();
            Exceptions.throwBizException(ErrCode.JSON_PASE_ERR);
        }
        return list;
    }

    /**
     * json字符串转为map泛型对象
     *
     * @param content json字符串
     * @return 转换后的对象
     */
    public static Map<String, Object> readValueToMap(String content) {
        Map<String, Object> map = null;
        try {
            map = new ObjectMapper().readValue(content, new TypeReference<Map<String, Object>>() {

            });
        } catch (JsonProcessingException e) {
            e.printStackTrace();
            Exceptions.throwBizException(ErrCode.JSON_PASE_ERR);
        }
        return map;
    }

    /**
     * json字符串转为指定类型
     *
     * @param content   json字符串
     * @param classType 需要转换的类型
     * @return 转换后的对象
     */
    public static <T> T readValue(String content, Class<T> classType) {
        return ObjectMapperUtils.fromJSON(content, classType);
    }

    /**
     * json字符串转为指定类型
     *
     * @param content json字符串
     * @return 转换后的对象
     */
    public static <T> List<T> readListValue(String content, Class<T> classType) {
        return ObjectMapperUtils.readValue(content, classType);
    }
}

